package com.bw.announcement.announcement

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewModelScope
import com.bw.announcement.entity.AnnouncementData
import com.bw.announcement.entity.AnnouncementEntity
import com.bw.lib_base.base.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AnnouncementViewModel @Inject constructor(val announcementRepo: AnnouncementRepo):BaseViewModel() {
    private val _announcementData= MutableStateFlow<List<AnnouncementData>?>(null)
    val announcementData:MutableStateFlow<List<AnnouncementData>?> = _announcementData

    fun getAnnouncements(page:Int,size:Int){
        viewModelScope.launch {
            if (isInitLife()){
                lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED,{
                    val result = announcementRepo.getAnnouncements(page, size)
                    _announcementData.emit(result)
                })
            }
        }
    }


}